<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Shoutbox/classes/PhpFox_ServiceShoutbox.class.php, $Author:  $ */



/** Service for working with shoutbox and live chat functionality
 *
 * @package module.shoutbox
 * @version $Id:
 *
 */

class PhpFox_Mod_Shoutbox_ServiceShoutbox extends BaseService
{
    /**
     * Get shoutbox messages in admin extra tools Shoutbox
     *
     * @param array $aCond
     * @param integer $iPage
     * @param integer $iPageSize
     * @param string $sOrder
     * @return array
     */
    function &listMessages($aCond = array(), $iPage=0, $iPageSize=0, $sOrder = '')
    {
        $oShout = &$this->_oModule->getItem('Shoutbox');

        $aRes = $oShout->getList($aCond, $iPage, $iPageSize, $sOrder);
        return $aRes;
    }
    /**
     *  Delete message in in admin extra tools Shoutbox
     *
     *  @param mixed $mAccIds - message id
     *  @return boolean
     */
    function delete($mAccIds)
    {
        if (!is_array($mAccIds))
        {
            $mAccIds = array($mAccIds);
        }
        $oShout = $this->_oModule->getItem('Shoutbox');
        if ($mAccIds)
        {
            $oShout->delete($mAccIds);
            return true;
        }
    }
    /**
     * Get shoutbox (livechat) messages in public area
     *
     * @param integer $iGroupId
     * @return array
     */
    function getMessage($iGroupId = 0, $iRoomId = 0)
    {
        $aData = $this->oDatabase->getRows("
        	SELECT u.user, c.text, c.time FROM
        	". App::getT('chat') ." AS c
        		LEFT JOIN ". App::getT('user') ." AS u ON(c.userid = u.id)
        	WHERE c.group_id = '". $iGroupId ."' AND c.roomid = '". $iRoomId ."'
        	ORDER BY time DESC LIMIT 0,50
        ");
        foreach ($aData as $i=>$aItem)
        {
            $time[$i] = $aItem['time'];
            $row[$i] = $aItem['user'];
        }
        if(!empty($aData))
        {
            array_multisort($time, SORT_ASC, $row, SORT_ASC, $aData);
		}

        return $aData;
    }

    /**
     * Get shoutbox (livechat) messages in public area
     *
     * @param integer $iGroupId
     * @return array
     */
    function getShoutboxMessage()
    {
        $oShout = &$this->_oModule->getItem('Shoutbox');
        $sSql = 'SELECT `user`, `text`, `time` FROM '.App::getT('chat').' WHERE group_id = 0 AND roomid = 0 ORDER BY time DESC LIMIT 0,'.App::getParam('shoutbox.lim');
        $aData = $oShout->oDb->getRows($sSql);
        return $aData;
    }

    /**
     * Add new message
     *
     * @param string $sText
     * @param string $sUser
     * @param integer $iGroupId
     * @return boolean
     */
    function addMessage($sText, $iRoomId = 0, $iGroupId = 0)
    {
        // $oShout = &$this->_oModule->getItem('Shoutbox');
        /*
        // flood control
        $iLastTime = $oShout->oDb->getField('SELECT `time` FROM '.App::getT('chat').' WHERE `user` = "'.$sUser.'" ORDER BY `time` DESC LIMIT 0,1');
        $iGroupId = $iGroupId ? $iGroupId : '';
        $sText = strip_tags($sText);
        if((time()-$iLastTime) > App::getSetting('flood_time'))
        {
            $sSql = 'INSERT INTO '.App::getT('chat').'(`user`,`text`,`time`,`ip`,group_id)
                     VALUES ("'.$sUser.'","'.$oShout->oDb->escape($sText).'",'.time().',"'.$_SERVER['REMOTE_ADDR'].'","'.$iGroupId.'")';
            $oShout->oDb->query($sSql);
            return true;
        }
        return false;
        */
        App::loadClass('Text');
        $oTxt = new Text();
        $sText = $oTxt->parseText($oTxt->preParseText($sText));
        $sText = trim($sText);
        $this->oDatabase->insert(App::getT('chat'), array
        (
        	'userid' => App::getUser('id'),
        	'text' => $sText,
        	'time' => time(),
        	'ip' => $_SERVER['REMOTE_ADDR'],
        	'group_id' => $iGroupId,
        	'roomid' => $iRoomId,
        ));
		return true;
    }
    /**
     * Delete messages from Period
     *
     * @param string $sInterval
     * @return boolean
     */
    function deleteFromPeriod($sInterval)
    {
        $sSql = 'DELETE FROM '. App::getT('shoutbox') .' WHERE time < '.intval($sInterval);
        $this->oDatabase->query($sSql);
        return true;
    }
}
?>